from django.contrib.auth.mixins import LoginRequiredMixin, PermissionRequiredMixin
from django.http import HttpResponseRedirect
from django.urls import reverse
from django.views.generic import TemplateView
from lib.django.views import DetailView
from audit.lib.views import AuditorContextMixin

from lib.django.views import (
    AuditFormPostAPIView,
    HTMXCreateFormView,
    HTMXUpdateFormView,
    PermissionCheckMethodMixin,
    ReadOnlyFormMixin,
)

from .. import forms
from ..models import Project
from .lib import template_path


# ==============================================================================
# List
# ==============================================================================
class ProjectListView(
    LoginRequiredMixin,
    TemplateView
):
    template_name = template_path('project/list.html')

# ==============================================================================
# Detail
# ==============================================================================
class ProjectCreateView(LoginRequiredMixin, HTMXCreateFormView):
    form_class = forms.ProjectForm
    form_action_view_name = "project:create"

    def get_success_url(self):
        id = getattr(self.object, 'id')
        return reverse("project:detail", kwargs={'pk': id})

    def form_valid(self, form):
        user = self.request.user
        project = form.save(commit=False)
        project.creator = user
        project.save()
        form.save_m2m()
        self.object = project
        return HttpResponseRedirect(self.get_success_url())

class ProjectDetailView(
    LoginRequiredMixin,
    AuditorContextMixin,
    ReadOnlyFormMixin,
    DetailView,
    PermissionCheckMethodMixin
):
    model = Project
    context_object_name = "project"
    form_class = forms.ProjectForm
    template_name = template_path("project/detail.html")
    permission_required = "user.can_manage_projects"
    audit_permission = "user.can_manage_projects"
    perm_check_func_name = "can_view"


    def get_audit_form_action(self):
        pk = self.kwargs.get(self.pk_url_kwarg)
        return reverse("project:audit", kwargs={'pk': pk})


class ProjectUpdateView(
    LoginRequiredMixin,
    HTMXUpdateFormView,
    PermissionCheckMethodMixin
):
    model = Project
    form_class = forms.ProjectForm
    template_name = "form_base.html"
    permission_required = "user.can_manage_projects"
    perm_check_func_name = "can_edit"

    def get_success_url(self):
        id = getattr(self.object, 'id')
        return reverse("project:detail", kwargs={'pk': id})

    def get_form_action_view_name(self):
        pk = self.kwargs.get(self.pk_url_kwarg)
        return reverse("project:update", kwargs={'pk': pk})


class ProjecAuditFormPostAPIView(
    LoginRequiredMixin,
    PermissionRequiredMixin,
    AuditFormPostAPIView
):
    model = Project
    permission_required = "user.can_manage_projects"

# ==============================================================================
# Management
# ==============================================================================
class ProjectManagementView(
    LoginRequiredMixin,
    PermissionRequiredMixin,
    TemplateView
):
    permission_required = "user.can_manage_projects"
    template_name = template_path("management/list.html")
    
